package com.kabigon.tiny.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

/**
 * Bean工厂后处理器
 * BeanDefinitionMap 填充完后，Bean 实例化之前
 * @author kabigon
 * @version 2022/12/4/23:35
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("BeanDefinitionMap 填充完毕后回调该方法");
        // 修改某个BeanDefinition
        //BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userDao");
        //beanDefinition.setBeanClassName("com.kabigon.tiny.service.impl.UserServiceImpl");

        // 注册BeanDefinition
        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClassName("com.kabigon.tiny.dao.impl.PersonDaoImpl");
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        defaultListableBeanFactory.registerBeanDefinition("personDao", beanDefinition);

    }
}
